package com.stox.core.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.stox.core.dao.mapper.ScripRowMapper;
import com.stox.core.model.Scrip;
import com.stox.core.model.exception.StoxException;
import com.stox.design.DatabaseConfigurer;
import com.stox.ioc.Container;

public class ScripDao extends AbstractDao implements DatabaseConfigurer {
	
	private ScripRowMapper scripRowMapper;
	
	public ScripDao() {
		this.scripRowMapper = Container.get(ScripRowMapper.class);
	}
	
	@Override
	public int getOrder() {
		return 0;
	}
	
	@Override
	public Object invoke(List<String> payload) {
		if(!payload.contains("SCRIP")){
			try {
				getConnection().createStatement().execute(ScripRowMapper.createSql);
				getConnection().createStatement().execute(ScripRowMapper.createIndexSql);
			} catch (SQLException e) {
				throw new StoxException(e);
			}
		}
		return null;
	}
	
	public void persist(Scrip scrip) throws SQLException{
			PreparedStatement ps = getConnection().prepareStatement(ScripRowMapper.insertSql);
			scripRowMapper.map(ps, scrip);
			ps.execute();
			commit();
	}
	
	public Scrip get(String code) throws SQLException{
		String sql = ScripRowMapper.selectSql+" where s.code = ?";
		PreparedStatement ps = getConnection().prepareStatement(sql);
		ps.setString(1, code);
		List<Scrip> scrips = scripRowMapper.map(ps.executeQuery());
		if(scrips.isEmpty()){
			return null;
		}
		return scrips.get(0);
	}
	
	public void updateMaxDate(String scripCode, Date maxDate) throws SQLException{
		String sql = "update scrip set maxDate = ? where code = ?";
		PreparedStatement ps = getConnection().prepareStatement(sql);
		ps.setDate(1, new java.sql.Date(maxDate.getTime()));
		ps.setString(2, scripCode);
		ps.executeUpdate();
	}
	
	public void persistAll(List<Scrip> scrips) throws SQLException{
		PreparedStatement ps = getConnection().prepareStatement(ScripRowMapper.insertSql);
		for(Scrip scrip : scrips){
				scripRowMapper.map(ps, scrip);
				ps.addBatch();
		}
		ps.executeBatch();
	}
	
	public List<Scrip> get() throws SQLException{
		return  scripRowMapper.map(
				getConnection().createStatement().executeQuery(ScripRowMapper.selectSql));
	}
	
	public List<Scrip> get(Date date) throws SQLException{
		String sql = ScripRowMapper.selectSql+", bar b where s.code=b.code and b.bardate = ?";
		PreparedStatement ps = getConnection().prepareStatement(sql);
		ps.setDate(1, new java.sql.Date(date.getTime()));
		return scripRowMapper.map(ps.executeQuery());
	}
	
	
	public List<String> getScripCodesForIndexCode(String indexCode) throws SQLException{
		List<String> scrips = new ArrayList<String>();
		PreparedStatement ps = getConnection().prepareStatement("select scripcode from index_scrip where indexcode = ?");
		ps.setString(1, indexCode);
		ResultSet rs = ps.executeQuery();
		while(rs.next()){
			scrips.add(rs.getString("scripcode"));
		}
		return scrips;
	}
	
	public List<String> getScripCodesForSectorCode(String sectorCode) throws SQLException{
		List<String> scrips = new ArrayList<String>();
		PreparedStatement ps = getConnection().prepareStatement("select scripcode from sector_scrip where sectorcode = ?");
		ps.setString(1, sectorCode);
		ResultSet rs = ps.executeQuery();
		while(rs.next()){
			scrips.add(rs.getString("scripcode"));
		}
		return scrips;
	}
}
